﻿@using System.Text
@{
    var part = (MultiChoiceVoting.Models.MultiChoiceVotingPart)Model.ContentPart;
    var radioButtonName = String.Format("voteFor{0}", part.Id);
    var votingDivId = String.Format("multichoicevoting_{0}", part.Id);
    var votingResultDivId = String.Format("{0}_result", votingDivId);
    var userName = HttpContext.Current.User.Identity.Name;
    var showResults = !(part.IsActive && DateTime.Now.ToLocalTime() > part.ReleaseDate && DateTime.Now < part.FinishDate);
    var targetUrl = Url.Action("Vote", "Voting", new { Area = "MultiChoiceVoting" }, null);

    var sb = new StringBuilder();

    foreach (var item in part.Answers)
    {
        sb.Append(String.Format("{0}: {1} vote(s)<br/>", item.Text, item.VoteCount));
    }
}

@{ Style.Require("MultiChoiceVoting"); }
@{ Script.Require("VotingScript").AtHead(); }


@part.Question<br />
<p class="multichoicevoting_comment">@part.Comment</p>
<div id="@votingDivId">
    <div class="multichoicevoting_answers">
        @for (var i = 0; i < part.Answers.Count(); i++)
        {
            <input type="radio" name="@radioButtonName" value="@i" />@part.Answers[i].Text<br />
        }
    </div>
    <div id="@votingResultDivId">
    </div>
    <br />
    @if (String.IsNullOrEmpty(userName))
    {
        @T("Login to vote.")
    }
    else if (DateTime.Now.ToLocalTime() < part.ReleaseDate)
    {
        @T(String.Format("Voting starts {0}.", part.ReleaseDate))
    }
    else if (DateTime.Now.ToLocalTime() > part.FinishDate)
    {
        @T(String.Format("Voting finished {0}.", part.FinishDate))
    }
    else if (!part.IsActive)
    {
        @T("Voting is inactive right now.")
    }
    else
    {
        <input type="button" value="@T("Vote")" onclick="@T(String.Format("$.MultiChoiceVoting.vote({0}, '{1}', '{2}', '{3}')", part.Id, radioButtonName, userName, targetUrl))" />
    }
</div>

@{
    
}
<script>
    $(document).ready(function () {
        var userName = '@userName';
        var showResults = @String.Format("{0}", showResults ? "true" : "false");
        if (userName) {
            if ($.MultiChoiceVoting.readvotingcookie('@part.Id', userName) != null || showResults) {
                $.MultiChoiceVoting.showvotingresult('@part.Id', '@T(sb.ToString())');
            }
        }
    });
</script>
